from flask import Flask,render_template,jsonify,request
from random import randint


app = Flask(__name__)


def rand_five_percent():
    rel = randint(1,5)
    if rel ==5:
        return True
    return False


def rand_ten_percent():
    rel = randint(1,10)
    if rel == 5:
        return True
    return False

def rand_fiften_percent():
    rel = randint(1,15)
    if rel == 5:
        return True
    return False

def rand_twenty_percent():
    rel = randint(1,20)
    if rel == 5:
        return True
    return False


@app.route("/")
def index():
    for x in range(10000):
        rand_five_percent()
    return "ok"

@app.route("/api/test/probability")
def random_app():

    value = request.values
    amount = value.get("amount")
    data = {
        "status":True
    }
    try:
        amount = int(amount)
    except Exception as e:
        data["status"] = False
        data["err_msg"] = e.args
        return jsonify(data)
    if amount<500:
        rel = rand_five_percent()
    elif amount >= 500 and amount< 1000:
        rel = rand_ten_percent()
    elif amount >1000 and amount <2000:
        rel = rand_fiften_percent()
    else:
        rel = rand_twenty_percent()

    data["data"] = {
        "win":rel
    }
    return jsonify(data)


@app.route("/api/login")
def login_func():
    forms = request.values
    username = forms.get("username")
    password = forms.get("password")
    print(username)
    print(password)
    if username=="wo" and password == "ni":
        return jsonify({"status":True})
    else:
        return jsonify({"status":False})

@app.route("/api/login2",methods=["POST","GET"])
def login_func2():
    if request.method == "POST":
        forms = request.values
        username = forms.get("username")
        password = forms.get("password")
        print(username)
        print(password)
        if username=="wo" and password == "ni":
            return jsonify({"status":True})
        else:
            return jsonify({"status":False})
    elif request.method == "GET":
        return jsonify({"status":False,"err_msg":"请求不合法"})




if __name__ == "__main__":
    app.run(host="0.0.0.0",port=8800,debug=True)
